import {
  getCommentConfigList,
  getFlowModelList,
  addComment,
  editComment,
  startStatus, deleteComment,
} from "../server/commentApi";

const initData = {
  commentConfigList: [],
  msgThemeTypeList: [],
  flowModelList: [],
  totalCount: 0,
};

export const COMMENT_CONFIG_MODEL_NAMESPACE = 'comment_config';
export const prefixedAction = name => `${COMMENT_CONFIG_MODEL_NAMESPACE}/${name}`;
export default {
  namespace: COMMENT_CONFIG_MODEL_NAMESPACE,
  state: {
    ...initData,
  },

  effects: {
    *getCommentConfigList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas, totalCount } = yield call(
        getCommentConfigList,
        payload,
      );
      yield put({ type: "get/getCommentConfigList", payload: datas, totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *getFlowModelList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas } = yield call(
          getFlowModelList,payload);
      yield put({ type: "get/getFlowModelList", payload: datas });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *addComment({ payload }, { call }) {
      const resp = yield call(addComment, payload);
      return resp;
    },
    *editComment({ payload }, { call }) {
      const resp = yield call(editComment, payload);
      return resp;
    },
    *deleteComment({ payload }, { call }) {
      const resp = yield call(deleteComment, payload);
      return resp;
    },
    *startStatus({ payload }, { call }) {
      const resp = yield call(startStatus, payload);
      return resp;
    },
  },
  reducers: {
    "get/getCommentConfigList": (state, { payload, totalCount }) => ({
      ...state,
      commentConfigList: payload,
      totalCount,
    }),
    "get/getFlowModelList": (state, { payload }) => ({
      ...state,
      flowModelList: payload,
    }),
    clearData: () => ({ ...initData }),
  },
};
